<?php

namespace App\Models;

use Jenssegers\Mongodb\Eloquent\SoftDeletes;

class Circle extends Base
{
    use SoftDeletes;

    const TYPE_AREA = 1;
    const TYPE_CATEGORY = 2;
    const TYPE_HOT_COLD = 3;

    const STATUS_WAIT_AUDIT = 1;
    const STATUS_SUCCESS = 2;
    const STATUS_FAIL = 3;

    const CAT_ID = 2289; // 猫咪宠友圈
    const DOG_ID = 2288; // 狗狗宠友圈


    const CACHE_CIRCLE_COVER_USER = 'circle_join_user_cover';
    const CACHE_CIRCLE_LOTTERY_USER = 'circle_lottery_user';

    protected $hidden=['pivot'];

    protected $casts = [
        'lottery' => 'array',
    ];

    protected $appends = ['activity_status'];

    public function setLotteryAttribute($val) {
        $this->attributes['lottery'] = $val;
    }


    public function is_focus() {
        return $this->hasOne(UserJoinCircle::class, 'circle_id')->where('is_activity', 0);
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function article() {
        return $this->belongsToMany(Article::class, 'article_relate_circles'
            , 'circle_id', 'article_id');
    }

    public function user_article() {
        return $this->article();
    }

    public function circle_category() {
        return $this->belongsToMany(CircleCategory::class, 'circle_category_relates'
            , 'circle_id', 'circle_category_id');
    }

    public function article_yesterday() {
        return $this->article();
    }

    public function join_users() {
        return $this->belongsToMany(User::class, 'user_join_circles'
            , 'circle_id', 'user_id');
    }

    public function getActivityStatusAttribute() {
        $cur_time = time();
        $start_time = strtotime($this->activity_start_at);
        $end_time = strtotime($this->activity_end_at);
        if ($cur_time < $start_time)
            return '未开始';
        else if( $cur_time >= $start_time && $cur_time <= $end_time)
            return '进行中';
        else {
            if ($this->is_publish_result)
                return '已开奖';
            else
                return '已截止';
        }

    }

}
